package es.usj.model;

import java.text.DateFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Alarma {
	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(byte month) {
		this.month = month;
	}

	public int getDay() {
		return day;
	}

	public void setDay(byte day) {
		this.day = day;
	}

	public int getHour() {
		return hour;
	}

	public void setHour(byte hour) {
		this.hour = hour;
	}

	public int getMinute() {
		return minute;
	}

	public void setMinute(byte minute) {
		this.minute = minute;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public boolean isRepeat() {
		return repeat;
	}

	public void setRepeat(boolean repeat) {
		this.repeat = repeat;
	}

	public int getrYear() {
		return rYear;
	}

	public void setrYear(int rYear) {
		this.rYear = rYear;
	}

	public int getrMonth() {
		return rMonth;
	}

	public void setrMonth(byte rMonth) {
		this.rMonth = rMonth;
	}

	public int getrDay() {
		return rDay;
	}

	public void setrDay(byte rDay) {
		this.rDay = rDay;
	}

	public int getrHour() {
		return rHour;
	}

	public void setrHour(byte rHour) {
		this.rHour = rHour;
	}

	public byte getrMinute() {
		return rMinute;
	}

	public void setrMinute(byte rMinute) {
		this.rMinute = rMinute;
	}

	private long id = 0;
	public int getSecond() {
		return second;
	}

	public void setSecond(int second) {
		this.second = second;
	}

	private int year = 0;
	private int month = 0;
	private int day = 0;
	private int hour = 0;
	private int minute = 0;
	private int second = 0;
	private String description = null;
	private String type = null;

	private boolean repeat = false;
	private int rYear = 0;
	private int rMonth = 0;
	private int rDay = 0;
	private int rHour = 0;
	private byte rMinute = 0;
	private int rSecond = 0;

	public int getrSecond() {
		return rSecond;
	}

	public void setrSecond(int rSecond) {
		this.rSecond = rSecond;
	}

	public void setDate(Date fecha) {
		Calendar calendario = Calendar.getInstance();
		calendario.setTimeInMillis(fecha.getTime());
		setCalendar(calendario);
	}

	public void setCalendar(Calendar fecha) {
		Calendar calendario = Calendar.getInstance();
		year = calendario.get(Calendar.YEAR);
		month = calendario.get(Calendar.MONTH);
		day = calendario.get(Calendar.DAY_OF_MONTH);

		hour = calendario.get(Calendar.HOUR_OF_DAY);
		minute = calendario.get(Calendar.MINUTE);
		second = calendario.get(Calendar.SECOND);
	}

	public Date getDate() {
		Calendar calendario = getCalendar();
		return new Date(calendario.getTimeInMillis());
	}

	public Calendar getCalendar() {
		Calendar calendario = Calendar.getInstance();
		calendario.set(year, month, day, hour, minute, second);
		return calendario;
	}

	public String getFullDateString() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy kk:mm:ss");
		return sdf.format(getCalendar()).toString();
	}

	public String getDateAndTimeString() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy kk:mm");
		return sdf.format(getCalendar()).toString();
	}

	public String getLocaleDateString() {
		DateFormat sdf = java.text.SimpleDateFormat.getDateTimeInstance();
		return sdf.format(getCalendar()).toString();
	}
	
	public String getDateString() {
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		return sdf.format(getCalendar()).toString();
	}

	public String getTimeString(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
		return sdf.format(getCalendar()).toString();
	}

	public long getId() {
		return id;
	}

	public void setId(long l) {
		this.id = l;
	}
	
	public String toXmlString() {
		StringBuffer res = new StringBuffer();
		this.setId(this.getCalendar().getTimeInMillis());
		res.append("<Alarma id=\"").append(id).append("\">");
/*		res.append(xmlTag("id", id));*/
		res.append(xmlTag("year", year));
		res.append(xmlTag("month", month));
		res.append(xmlTag("day", day));
		res.append(xmlTag("hour", hour));
		res.append(xmlTag("minute", minute));
		res.append(xmlTag("second", second));
		res.append(xmlTag("description", description));
		res.append(xmlTag("type", type));
		res.append(xmlTag("repeat", repeat));
		res.append(xmlTag("rYear", rYear));
		res.append(xmlTag("rMonth", rMonth));
		res.append(xmlTag("rDay", rDay));
		res.append(xmlTag("rHour", rHour));
		res.append(xmlTag("rMinute", rMinute));
		res.append(xmlTag("rSecond", rSecond));
		res.append("</Alarma>");
		return res.toString();
	}
	
	public String xmlTag(String tag, int value) {
		String res = "";
		if (tag != null) {
			res += "<" + tag + ">" + value + "</" + tag +">";
		}
		return res;
	}

	public String xmlTag(String tag, long value) {
		String res = "";
		if (tag != null) {
			res += "<" + tag + ">" + value + "</" + tag +">";
		}
		return res;
	}

	public String xmlTag(String tag, String value) {
		String res = "";
		if (tag != null) {
			res += "<" + tag + ">";
			if (value != null) {
				res += value;
			}
			res += "</" + tag +">";
		}
		return res;
	}

	public String xmlTag(String tag, boolean value) {
		String res = "";
		if (tag != null) {
			res += "<" + tag + ">" + String.valueOf(value) + "</" + tag +">";
		}
		return res;
	}

}